using micromsg;
using MicroMsg.Common.Event;
using MicroMsg.Common.Utils;
using MicroMsg.Manager;
using MicroMsg.Protocol;
using MicroMsg.Scene;
using MicroMsg.Storage;
using System;
using System.Collections.Generic;
using System.IO.IsolatedStorage;
namespace MicroMsg.Plugin.Sns.Scene
{
	public class NetSceneSnsUpload : NetSceneBaseEx<SnsUploadRequest, SnsUploadResponse, SnsUploadRequest.Builder>
	{
		private const string TAG = "NetSceneSnsUpload";
		public const int MAX_TIMES = 30;
		public onSceneFinishedUploadDelegate mOnSceneFinished;
		public uint mTimesToServer;
		public byte[] mDatabuf;
		public int mDataTotalLength;
		public string mClientMsgId = "";
		private int mSendDataLength = 8192;
		public int mStartPos;
		private SnsMediaTrans mSnsImgTrans;
		private SnsPostContext mPostContext;
		public bool doScene(SnsPostContext context, SnsMediaTrans imgTrans)
		{
			if (context == null || imgTrans == null)
			{
				return false;
			}
			this.mSendDataLength = Util.getSendPackSize();
			this.mSnsImgTrans = imgTrans;
			this.mPostContext = context;
			this.mStartPos = 0;
			this.mTimesToServer = 0u;
			this.mDatabuf = SnsMediaStorage.load(imgTrans.strfileName);
			if (this.mDatabuf != null && this.mDatabuf.Length > 0)
			{
				this.mDataTotalLength = this.mDatabuf.Length;
				this.mClientMsgId = imgTrans.strClientID;
				imgTrans.nStatus = 2;
				return this.doSceneEx();
			}
			return false;
		}
		private bool doSceneEx()
		{
			base.beginBuilder();
			this.mTimesToServer += 1u;
			this.mBuilder.BaseRequest = NetSceneBase.makeBaseRequest(0);
			this.mBuilder.Type = 2u;
			this.mBuilder.StartPos = (uint)this.mStartPos;
			this.mBuilder.TotalLen = (uint)this.mDataTotalLength;
			this.mBuilder.Buffer = this.getRemainDataToSend();
			this.mBuilder.ClientId = this.mClientMsgId;
			this.mBuilder.FilterStype = 0u;
			this.mBuilder.SyncFlag = this.mPostContext.sync2WeiboFlag;
			this.mBuilder.Description = this.mPostContext.strContent;
			this.mSessionPack.mCmdID = 95;
			base.endBuilder();
			return true;
		}
		protected override void onFailed(SnsUploadRequest request, SnsUploadResponse response)
		{
			Log.smethod_1("NetSceneSnsUpload", "send request failed");
			this.onSceneComplete(false);
			EventCenter.postEvent(EventConst.ON_NETSCENE_SNS_UPLOAD_ERR, RetConst.MM_ERR_CLIENT, null);
		}
		public byte[] loadImge(string fileName)
		{
			byte[] result;
			try
			{
				using (IsolatedStorageFile userStoreForApplication = IsolatedStorageFile.GetUserStoreForApplication())
				{
					if (!userStoreForApplication.FileExists(fileName))
					{
						result = null;
					}
					else
					{
						IsolatedStorageFileStream isolatedStorageFileStream = userStoreForApplication.OpenFile(fileName, 3, 1);
						if (isolatedStorageFileStream == null)
						{
							Log.smethod_1("NetSceneSnsUpload", "Can not Create Image thumb file");
							result = null;
						}
						else
						{
							byte[] array = new byte[isolatedStorageFileStream.get_Length()];
							int num = isolatedStorageFileStream.Read(array, 0, (int)isolatedStorageFileStream.get_Length());
							if (num > 0)
							{
								result = null;
							}
							else
							{
								result = array;
							}
						}
					}
				}
			}
			catch (Exception)
			{
				result = null;
			}
			return result;
		}
		protected override void onSuccess(SnsUploadRequest request, SnsUploadResponse response)
		{
			RetConst ret = (RetConst)response.BaseResponse.Ret;
			if (ret != RetConst.MM_OK)
			{
				Log.smethod_1("NetSceneSnsUpload", "send request failed ret =" + ret);
				this.onSceneComplete(false);
				EventCenter.postEvent(EventConst.ON_NETSCENE_SNS_UPLOAD_ERR, ret, null);
				return;
			}
			if (this.mTimesToServer > 30u)
			{
				this.onSceneComplete(false);
				EventCenter.postEvent(EventConst.ON_NETSCENE_SNS_UPLOAD_ERR, RetConst.MM_ERR_CLIENT, null);
				return;
			}
			if (!this.isSendCompleted())
			{
				if (!this.doSceneEx())
				{
					this.onSceneComplete(false);
					Log.smethod_1("NetSceneSnsUpload", "doSceneEx failed ");
					EventCenter.postEvent(EventConst.ON_NETSCENE_SNS_UPLOAD_ERR, RetConst.MM_ERR_CLIENT, null);
				}
				return;
			}
			if (this.mSnsImgTrans.strClientID.Equals(response.ClientId))
			{
				if (!string.IsNullOrEmpty(response.BufferUrl.Url))
				{
					SnsMediaStorage.addUrlMap(response.BufferUrl.Url, this.mSnsImgTrans.strfileName);
					this.mSnsImgTrans.strUrl = response.BufferUrl.Url;
					this.mSnsImgTrans.nUrlType = (int)response.BufferUrl.Type;
				}
				this.mSnsImgTrans.nSendLenth = (int)response.StartPos;
				if (response.ThumbUrlsList.get_Count() > 0)
				{
					using (IEnumerator<SnsBufferUrl> enumerator = response.ThumbUrlsList.GetEnumerator())
					{
						while (enumerator.MoveNext())
						{
							SnsBufferUrl current = enumerator.get_Current();
							if (current != null)
							{
								this.mSnsImgTrans.strThumbUrl = current.Url;
								this.mSnsImgTrans.nThumbUrlType = (int)current.Type;
								SnsMediaStorage.addUrlMap(current.Url, this.mSnsImgTrans.strThumbFileName);
								break;
							}
						}
					}
				}
				TimeLineObject timeLine = SnsInfoMgr.getTimeLine(this.mPostContext.localSnsObject);
				using (List<TloMedia>.Enumerator enumerator2 = timeLine.content.mediaList.GetEnumerator())
				{
					while (enumerator2.MoveNext())
					{
						TloMedia current2 = enumerator2.get_Current();
						if (current2 != null && current2.strUrl == this.mSnsImgTrans.strfileName)
						{
							current2.strUrl = this.mSnsImgTrans.strUrl;
							current2.nUrlType = this.mSnsImgTrans.nUrlType;
							current2.strThumb = this.mSnsImgTrans.strThumbUrl;
							current2.nThumbType = this.mSnsImgTrans.nThumbUrlType;
							current2.nID = response.Id;
							break;
						}
					}
				}
				SnsInfoMgr.setTimeLine(this.mPostContext.localSnsObject, timeLine);
			}
			this.onSceneComplete(true);
			EventCenter.postEvent(EventConst.ON_NETSCENE_SNS_UPLOAD_SUCCESS, ret, null);
		}
		public SKBuiltinBuffer_t getRemainDataToSend()
		{
			if (this.mStartPos < 0)
			{
				return Util.toSKBuffer("");
			}
			if (this.mDataTotalLength <= 0)
			{
				return Util.toSKBuffer("");
			}
			if (this.mStartPos >= this.mDataTotalLength)
			{
				return Util.toSKBuffer("");
			}
			if (this.mDatabuf == null)
			{
				return Util.toSKBuffer("");
			}
			if (this.mDataTotalLength - this.mStartPos < this.mSendDataLength)
			{
				this.mSendDataLength = this.mDataTotalLength - this.mStartPos;
			}
			return Util.toSKBuffer(this.mDatabuf, this.mStartPos, this.mSendDataLength);
		}
		public bool isSendCompleted()
		{
			this.mStartPos += this.mSendDataLength;
			return this.mStartPos == this.mDataTotalLength;
		}
		public void onSceneComplete(bool isSuccess)
		{
			this.mSnsImgTrans.nRetryTimes++;
			if (isSuccess)
			{
				this.mSnsImgTrans.nStatus = 5;
			}
			else
			{
				this.mSnsImgTrans.nStatus = 4;
			}
			this.mPostContext.mSnsTrans.nObjectstatus = this.mPostContext.mSnsObjectStatus;
			this.mPostContext.mSnsTrans.snsImgTransList = this.mPostContext.mSnsTrans.snsImgTransList;
			StorageMgr.snsTrans.update(this.mPostContext.mSnsTrans);
			this.mOnSceneFinished(this.mPostContext, this.mSnsImgTrans, this, isSuccess);
		}
	}
}
